package com.eos.inter.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eos.common.tool.DaoAccessObjectPoolContextFactory;
import com.eos.orm.mybatis.dao.ServiceFeeMonthReportMapper;
import com.eos.orm.mybatis.model.ServiceFeeMonthReport;
import com.hf.utils.StringUtil;

/**
 * 融信服务费的结算（每月5日提供，上月数据）接口
 * 
 * @author sz
 * 
 */
public class ServiceFeeMonthReportServlet extends BaseHttpServlet {

	private static final long serialVersionUID = 1L;
	private ServiceFeeMonthReportMapper mapper = (ServiceFeeMonthReportMapper) DaoAccessObjectPoolContextFactory
			.getInstaAccessObjectPoolContext().instanceDataAccessObject(
					"ServiceFeeMonthReportMapper");

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ServiceFeeMonthReport serviceFeeMonthReport = new ServiceFeeMonthReport();// 添加数据的对象
		serviceFeeMonthReport.setCustomerId(StringUtil.parseInt(StringUtil
				.NullToString(request.getParameter("customer_id"))));// 区分哪个结算客户
		serviceFeeMonthReport.setPartnerName(StringUtil.NullToString(request
				.getParameter("parnter_name")));//
		serviceFeeMonthReport.setCustName(StringUtil.NullToString(request
				.getParameter("cust_name")));//
		serviceFeeMonthReport.setCustIdCard(StringUtil.NullToString(request
				.getParameter("cust_id_card")));
		serviceFeeMonthReport.setTaobaoOrderId(StringUtil.NullToString(request
				.getParameter("taobao_order_id")));
		serviceFeeMonthReport.setCustContractPhone(StringUtil
				.NullToString(request.getParameter("cust_contract_phone")));
		serviceFeeMonthReport.setPackageTotalAmount(StringUtil
				.NullToString(request.getParameter("package_total_amount")));
		serviceFeeMonthReport.setPackageType(StringUtil.NullToString(request
				.getParameter("package_type")));
		serviceFeeMonthReport.setCurPackageAmount(StringUtil
				.NullToString(request.getParameter("cur_package_amount")));
		serviceFeeMonthReport.setCustCurFee(StringUtil.NullToString(request
				.getParameter("cust_cur_fee")));
		serviceFeeMonthReport.setCurServiceFee(StringUtil.NullToString(request
				.getParameter("cur_service_fee")));
		serviceFeeMonthReport.setReportMonth(StringUtil.NullToString(request
				.getParameter("report_month")));
		serviceFeeMonthReport.setInsertTime(StringUtil.stringDate(new Date()));
		serviceFeeMonthReport.setStatus("0");

		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
			if (identification(serviceFeeMonthReport.getCustContractPhone(),
					serviceFeeMonthReport.getCustIdCard())) {
				// 失败返回1
				out.write("1".toString().getBytes("utf-8"));
				return;
			}
			if (0 != serviceFeeMonthReport.getCustomerId()
					&& mapper.insert(serviceFeeMonthReport) == 0) {
				// 成功返回0
				out.write("0".toString().getBytes("utf-8"));
			} else {
				// 失败返回1
				out.write("1".toString().getBytes("utf-8"));
			}
		} catch (Exception e) {
			log
					.error("com.eos.inter.servlet.ServiceFeeMonthReportServlet.doPost throws exception:"
							+ e.getMessage());
		} finally {
			if (null != out) {
				out.flush();
				out.close();
			}
		}
	}
}
